import { AnyAction, Reducer } from 'redux';
import { message } from 'antd';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import {
  queryPartitionManagement,
  queryLogistics,
  queryGardenList,
  operChangeStatus,
  savePartition,
  updatePartition,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchLogs: Effect;
    fetchGarden: Effect;
    change: Effect;
    saveAndUpdate: Effect;
  };
  reducers: {
    save: Reducer;
    setLogs: Reducer;
    setGarden: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'partitionManagement',

  state: {
    dataInfo: {
      count: 0,
      list: [],
    },
    logsList: [],
    gardenList: [],
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryPartitionManagement, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: response.body,
      });
    },

    * fetchLogs({ payload }, { call, put }) {
      payload = {
        pageIndex: 1,
        pageSize: 20,
        ...payload,
      };
      const response = yield call(queryLogistics, payload);
      yield put({
        type: 'setLogs',
        payload: response.body ? response.body.list : [],
      });
    },

    * fetchGarden({ payload }, { call, put }) {
      payload = {
        pageIndex: 1,
        pageSize: 20,
        ...payload,
      };
      const response = yield call(queryGardenList, payload);
      yield put({
        type: 'setGarden',
        payload: response.body ? response.body.list : [],
      });
    },

    * change({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operChangeStatus, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('设置成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },

    * saveAndUpdate({ payload: { search, ...query } }, { call, put }) {
      const formData = new FormData();
      Object.entries(query).map((item: any) => (
        formData.append(item[0], item[1])
      ));
      const response = yield call(query.id ? updatePartition : savePartition, { body: formData });
      const data = fetchValidation(response);
      if (data) {
        message.success('设置成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        dataInfo: action.payload,
      };
    },

    setLogs(state, { payload }) {
      return {
        ...state,
        logsList: payload,
      };
    },

    setGarden(state, { payload }) {
      return {
        ...state,
        gardenList: payload,
      };
    },
  },
};

export default Model;
